package com.utils
{
	import com.org.adobe.serialization.json.JSON;
	import com.org.greensock.events.LoaderEvent;
	import com.org.greensock.loading.DataLoader;
	import com.org.greensock.loading.LoaderMax;
	
	import flash.net.URLRequest;
	
	/**
	 * (批量/单个)JSON文件加载
	 * @author Mr.Lu
	 * <ul> 
	 * <li><strong>第一步:设置相关参数</strong></li>
	 * <li>prefix:如果是网络路径需要设置前缀“assets”之前的内容;</li>
	 * <li>PREFIX_TAG:"assets/data/" 前缀</li>
	 * <br /><br />
	 * <li><strong>第二步:设置加载列表</strong></li>
	 * <li>startLoad(nameList) 将要加在的json文件放入数组内传入</li>
	 * </ul>
	 */	
	public class JsonUtil
	{
		public static var prefix:String = '';
		private var _prefixTag:String = 'assets/data/';
		private static var _sourceBox:Object = {};
		private static var _stringSourceBox:Object = {};//观察json文件内部结构用
		
		private var _designNameList:Array = [];
		private var _laderMax:LoaderMax;
		private var _loaderOver:Boolean = false;
		private var _jsonUtilCallBack:Function = new Function(); 
		
		private static var jsonUtil:JsonUtil;
		public function JsonUtil()
		{
			if(!jsonUtil) jsonUtil = this;
		}
		
		public static function get instance():JsonUtil{
			return jsonUtil || new JsonUtil();
		}
		
		
		
		
		
		/**
		  * ///////////////////////////////////////////////
		 * 开始加载JSON文件，可以是多个
		 * ///////////////////////////////////////////////
		 * @param params 文件名
		 */		
		public function startLoad(...params):void
		{
			_designNameList = params;
			_laderMax = new LoaderMax({onComplete:completeHandler});
			for (var i:int = 0; i < _designNameList.length; i++) 
			{
				append(_designNameList[i]);
			}
			_laderMax.load();
		}
		
		private function append(name:String):void{
			_laderMax.append( new DataLoader(new URLRequest(prefix + _prefixTag + name + '.json')) );
		}
		
		/**加载完毕执行,将加载的JSON数据存入资源池*/		
		private function completeHandler(e:LoaderEvent):void 
		{ 
			_loaderOver = true;
			for (var i:int = 0; i < _designNameList.length; i++) 
			{
				_sourceBox[_designNameList[i]] = dataArrayFormat(com.org.adobe.serialization.json.JSON.decode(e.target.content[i]));//转化为数组
				_stringSourceBox[_designNameList[i]] = e.target.content[i];//转化为数组
				trace('【' + _designNameList[i] + '】读取完毕！');
			}
			_jsonUtilCallBack();
		}
		
		/**将Object转化成Array*/		
		private function dataArrayFormat(target:Object):Array
		{
			var tempArr:Array = [];
			for(var key:* in target){
				tempArr.push({key:key,value:target[key]});
			}
			return tempArr;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/**
		 * ///////////////////////////////////////////////
		 * 根据json文件名获取json数据
		 * ///////////////////////////////////////////////
		 * @param name json文件名
		 * @param key 每一项对应主键
		 */		
		public function load(name:String,key:* = null):*
		{
			if(!_loaderOver) throw Error('资源尚未加载完毕!');
			if(!key){
				return _sourceBox[name];
			}else{
				return getResultData(_sourceBox[name],key);
			}
		}
		
		/**类型不确定,可能是Array也可能是Object*/		
		private function getResultData(target:Array,key:* = null):*
		{
			for (var i:int = 0; i < target.length; i++) 
			{
				if(target[i]['key'] == key) return target[i].value;
			}
			return null;
		}
		
		
		
		
		
		/**资源加载完成后调用此函数*/		
		public function set jsonUtilCallBack(value:Function):void
		{
			_jsonUtilCallBack = value;
		}
		
		
		
		
		/**打印指定json文件内部数据结构*/		
		public function traceJsonStructure(name:String):void
		{
			if(!_loaderOver) throw Error('资源尚未加载完毕!');
			trace(_stringSourceBox[name]);
		}
		
	}
}